package org.ainlolcat.ainscience.UI.renderer;

import org.math.plot.Plot2DPanel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;

/**
 * @author ainlolcat
 *         Date: 5/9/13
 */
public class LineDataRenderer extends JPanel{
    LineRendererSource source;
    JList jlist = new JList();

    public LineDataRenderer(LineRendererSource source){
        this.source = source;
        setLayout(new BorderLayout());
        initComponent();
    }

    private void initComponent(){
        Vector data = new Vector<String>(source.getNames());
        jlist.setListData(data);
        jlist.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String value = (String)jlist.getSelectedValue();
                plotFor(value);
            }
        });
        JScrollPane scroll = new JScrollPane(jlist);
        add(scroll, BorderLayout.LINE_START);
    }

    private void plotFor(String name){
        double[][] data = source.getData(name);
        double[] x = new double[data[0].length];
        double[] y = new double[data[0].length];
        double[] I = new double[data[0].length];
        for (int i=0;i<data[0].length;i++){
            x[i] = data[0][i];
            y[i] = data[1][i];
            I[i] = data[2][i];
        }
        Plot2DPanel plot = new Plot2DPanel();
        plot.addLinePlot("", x, y);
        plot.addLinePlot("", x, I);
        add(plot, BorderLayout.CENTER);
        getTopLevelAncestor().setVisible(true);
    }
}
